package com.wxy.ideaplugin.utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JdbcUtils {

    public static Connection getConnection() {
        String url = "jdbc:mysql://127.0.0.1:3309/db_lowcode?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&pinGlobalTxToPhysicalConnection=true&useSSL=false";
        String user = "root";
        String password = "123456";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void executeUpdate(String sql) {
        Connection conn = getConnection();
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<Map<String,Object>> executeQuery(String sql, List paramList) {
        Connection conn = getConnection();
        Statement stmt = null;
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            // 设置查询参数
            if (paramList!=null && !paramList.isEmpty()) {
                for (int i=0;i<paramList.size();i++) {
                    pstmt.setObject(i+1, paramList.get(i));
                }
            }
            // 执行查询
            ResultSet resultSet = pstmt.executeQuery();
            //获取结果集元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            while (resultSet.next()) {
                Map<String, Object> map = new HashMap<>();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    map.put(metaData.getColumnName(i), resultSet.getObject(i));
                }
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public static Long executeInsert(String sql, List paramList) {
        Connection conn = getConnection();
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            // 设置查询参数
            if (paramList!=null && !paramList.isEmpty()) {
                for (int i=0;i<paramList.size();i++) {
                    pstmt.setObject(i+1, paramList.get(i));
                }
            }
            // 执行查询
            pstmt.executeUpdate();
            // 获取生成的主键
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                Long generatedId = rs.getLong(1);
                return generatedId;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static Long executeUpdate(String sql, List paramList) {
        Connection conn = getConnection();
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            // 设置查询参数
            if (paramList!=null && !paramList.isEmpty()) {
                for (int i=0;i<paramList.size();i++) {
                    pstmt.setObject(i+1, paramList.get(i));
                }
            }
            // 执行查询
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }



    public static void main(String[] args) {
        try {
            Connection conn = getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                System.out.println("ID: " + id + ", Name: " + name);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
